<?php
/**
 * 命令模式
 *
 * 把发出命令的责任和执行命令的责任分割开，委派给不同的对象
 */
//具体命令的抽象角色
abstract class AbstractCommand{
	protected $receiver;
	//负责接收者的相应操作
	public function __construct(Receiver $receiver){
		$this->receiver = $receiver;
	}
	abstract public function Execute();
}
//具体命令角色
class ConcreteCommand extends AbstractCommand
{
	public function Execute()
	{
		$this->receiver->Action();
	} 
}
//接收者
class Receiver
{
	public function Action()
	{
		echo "Called Receiver Action";
	}
}
//请求者
class Invoker{
	//命令的引用
	private $command;
	//设置命令
	public function SetCommond(AbstractCommand $command)
	{
		$this->command = $command;
	}
	//执行命令
	public function ExecuteCommand()
	{
		$this->command->Execute();
	}
}
//创建一个接收者，命令和请求者
$r = new Receiver();
$c = new ConcreteCommand($r);
$i = new Invoker();
//设置和发出命令
$i->SetCommond($c);
$i->ExecuteCommand();
